package kr.hkit.androidcomplete01.ch12;

import java.util.ArrayList;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ListIconText extends Activity {
	private ArrayList<MyItem> arItems;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		arItems = new ArrayList<ListIconText.MyItem>();
		MyItem item;
		item = new MyItem(R.drawable.dog, "삼성 개");
		arItems.add(item);
		item = new MyItem(R.drawable.androboy, "엘지 개");
		arItems.add(item);
		item = new MyItem(R.drawable.korandoc, "코란도");
		MyListAdapter adapter = new MyListAdapter();
		
	}
	
	public class MyListAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			Log.d("MyListAdapter", "call-getcount()");
			return arItems.size();
		}

		@Override
		public Object getItem(int position) {
			Log.d("MyListAdapter", "call-getItem()");
			return arItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			Log.d("MyListAdapter", "call-getItemId()");
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.d("MyListAdapter", "call-getView()");
			final int pos = position;
			if(convertView == null){
				LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.ch12_icontext, parent, false);
			}
			ImageView imgView = (ImageView) convertView.findViewById(R.id.img);
			imgView.setImageResource(arItems.get(position).iconSrc);
			
			TextView tv = (TextView) convertView.findViewById(R.id.text);
			tv.setText(arItems.get(position).strItem);
			
			Button btn = (Button) convertView.findViewById(R.id.btn);
			btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String text = arItems.get(pos).strItem + "를 주문합니다";
					Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
				}
			});
			return convertView;
		}		
	}
	
	public class MyItem{
		private int iconSrc;
		private String strItem;
		
		public MyItem(int iconSrc, String strItem) {
			this.iconSrc = iconSrc;
			this.strItem = strItem;
		}
	}
}
